//
//  NavView.m
//  MoreStarLiveTelecast
//
//  Created by babbage on 16/8/21.
//  Copyright © 2016年 babbage. All rights reserved.
//

#import "NavView.h"

@interface NavView ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIButton *leftBtn;

@property (nonatomic, strong) UIButton *rightBtn;

@end

@implementation NavView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}
//给自定义导航栏初始化
-(void)setupInit
{
    if (!GJCFStringIsNull(self.titleString)) {
        self.titleLabel.text = self.titleString;
        self.titleLabel.font = [UIFont systemFontOfSize:18];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    if (!GJCFStringIsNull(self.leftImgNameStr)) {
        [self.leftBtn setImage:[UIImage imageNamed:self.leftImgNameStr] forState:UIControlStateNormal];
        [self.leftBtn setImage:[UIImage imageNamed:self.leftImgNameStr] forState:UIControlStateSelected];
        [self.leftBtn addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
        self.leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    if (!GJCFStringIsNull(self.rightString)) {
        [self.rightBtn setTitle:self.rightString  forState:UIControlStateNormal];
        [self.rightBtn setTitle:self.rightString forState:UIControlStateSelected];
        self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.rightBtn addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
        self.rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
}
#pragma mark --- 点击导航栏按钮 ---
-(void)clickLeft
{
    self.leftBlock();
}
-(void)clickRight
{
    self.rightBlock();
}
#pragma mark --- 布局 ---
-(void)createUI
{
    [self addSubview:self.titleLabel];
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    [self navViewMakeConstraints];
}
#pragma mark --- 自动布局 ---
-(void)navViewMakeConstraints
{
    UIView *superview = self;
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(superview);
        make.left.equalTo(superview).offset(Margin);
        make.right.equalTo(self.titleLabel.mas_left).offset(-Space);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);//居中
        make.top.and.bottom.equalTo(self.leftBtn);
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(superview);//y居中
        make.left.equalTo(self.titleLabel.mas_right).offset(Space);
        make.right.equalTo(superview).offset(-Margin);
        make.height.equalTo(self.titleLabel);
    }];
}
#pragma mark --- 懒加载 ---
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    }
    return _titleLabel;
}
-(UIButton *)leftBtn
{
    if (!_leftBtn) {
        _leftBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    }
    return _leftBtn;
}
-(UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    }
    return _rightBtn;
}



@end
